﻿using System.Threading;
using Kugar.Core.BaseStruct;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Linq;

namespace Kugar.Tool.DataSync
{
    /// <summary>
    /// 导出触发器插件接口
    /// </summary>
    public interface IExportTriggerPlugin
    {
        /// <summary>
        /// 插件在初始化之后,由框架调用该函数,将触发函数传入,在触发器接收到导出需求时,由当前插件调用传入的函数
        /// </summary>
        /// <param name="data"></param>
        void OnTrigger(OnTriggerFunc invoker);
    }

    public abstract class TimerExportTriggerPlugin : IExportTriggerPlugin
    {
        private OnTriggerFunc _invoker = null;

        /// <summary>
        /// 定时间隔
        /// </summary>
        /// <param name="intervalOnSec"></param>
        public TimerExportTriggerPlugin(int intervalOnSec)
        {
            TimerPool.Default.Add(intervalOnSec*1000, timer_OnTimeToTrigger);
        }

        private void timer_OnTimeToTrigger(object state)
        {
            OnTimer();
        }

        void IExportTriggerPlugin.OnTrigger(OnTriggerFunc invoker)
        {
            _invoker = invoker;
        }

        protected void Trigger(string targetID, string dataTypeName, JObject data)
        {
            _invoker(targetID, dataTypeName, data);
        }

        protected abstract void OnTimer();



    }
}